<?php 

function count_karyawan_cuti($tanggal){

	$cuti = my_query("SELECT * FROM  surat_cuti WHERE '".$tanggal."' BETWEEN tanggal_mulai AND tanggal_selesai");
	return  my_num_rows($cuti);
}

function count_karyawan_spd($tanggal){

	$spd = my_query("SELECT * FROM spd WHERE tipe_karyawan='Karyawan' AND '".$tanggal."' BETWEEN tanggal_mulai AND tanggal_selesai");
	return  my_num_rows($spd);
}

function count_karyawan(){
	$result = my_query("SELECT karyawan_id FROM karyawan 
		WHERE karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
		AND karyawan_id NOT IN (select karyawan_id FROM pemberhentian)");
	return my_num_rows($result);
}

function count_karyawan_mpp(){

	$result = my_query("SELECT a.karyawan_id 
		FROM karyawan a 
		INNER JOIN biodata b ON a.biodata_id=b.biodata_id 
		WHERE karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
		AND karyawan_id NOT IN (select karyawan_id FROM pemberhentian) 
		AND DATE_ADD(b.tanggal_lahir,INTERVAL 55 YEAR) <= '".date('Y-m-d')."'"
		);
	return my_num_rows($result);
}

function page_block_design(){
$total_karyawan = count_karyawan();
$total_karyawan_label = ($total_karyawan > 0) ? $total_karyawan. ' Orang' : 'Tidak ada';

$total_karyawan_cuti = count_karyawan_cuti(date('Y-m-d'));
$total_karyawan_cuti_label = ($total_karyawan_cuti > 0) ? $total_karyawan_cuti. ' Orang' : 'Tidak ada';

$total_karyawan_aktif = $total_karyawan - $total_karyawan_cuti;
$total_karyawan_aktif_label = ($total_karyawan_aktif > 0) ? $total_karyawan_aktif. ' Orang' : 'Tidak ada';

$total_karyawan_mpp = count_karyawan_mpp();
$total_karyawan_mpp_label = ($total_karyawan_mpp > 0) ? $total_karyawan_mpp. ' Orang' : 'Tidak ada';

$aaaa = detail_header_view( '#FFFFFF'  );
$aaaa .= detail_rows_view( '<b>Total karyawan</b>' ,  ': '.$total_karyawan_label  ,false , "50%", "50%");
//$aaaa .= detail_rows_view( '<b>Aktif</b>' , ': '.$total_karyawan_aktif_label, false , "50%", "50%" );
$aaaa .= detail_rows_view( '<b>Sedang cuti</b>' ,': '.$total_karyawan_cuti_label   ,false , "50%", "50%"); 
$aaaa .= detail_rows_view( '<b>Persiapan pensiun</b>' ,  ': '.$total_karyawan_mpp_label  ,false , "50%", "50%");
$aaaa .= detail_footer_view();
$design1 ='
<div style="background:#fff;width:95%;height:100%;text-align:left;border:2px solid #CDCDCD;padding:3px;"> 
<div style="width:90px; height:72px; text-align:center;	border:#CDCDCD 1px solid;margin:2px;	float:left; font-size:9px; font-family:verdana;">
	<a href="index.php?com=surat_cuti" style="border:0;color:#fff"><img src="templates/hrisrskm/icons/a_cuti.png" width="55px">
	<div style="width:100%; color:#fff;height:12px; padding-top:5px;background-color:grey;">
		<b>Manage Cuti</b>
	</div></a>
</div>
<div style="width:90px;	height:72px; text-align:center;	border:#CDCDCD 1px solid;margin:2px;	float:left; font-size:9px; font-family:verdana;">
	<a href="index.php?com=spl" style="border:0;color:#fff"><img src="templates/hrisrskm/icons/a_spl.png" width="55px">
	<div style="width:100%; color:#fff;height:12px; padding-top:5px;background-color:grey;">
		<b>Manage SPL</b>
	</div></a>
</div>
<div style="width:90px;	height:72px; text-align:center;	border:#CDCDCD 1px solid;margin:2px;	float:left; font-size:9px; font-family:verdana;">
	<img src="templates/hrisrskm/icons/phk.png" width="55px">
	<div style="width:100%; color:#fff;height:12px; padding-top:5px;background-color:grey;">
		<b>Karyawan PHK</b>
	</div>
</div>
<div style="width:90px;	height:72px; text-align:center;	border:#CDCDCD 1px solid;margin:2px;	float:left; font-size:9px; font-family:verdana;">
	<img src="templates/hrisrskm/icons/perjalanadinas.png" width="55px">
	<div style="width:100%; color:#fff;height:12px; padding-top:5px;background-color:grey;">
		<b>Perjalanan Dinas</b>
	</div>
</div>

<div style="width:90px;	height:72px; text-align:center;	border:#CDCDCD 1px solid;margin:2px;	float:left; font-size:9px; font-family:verdana;">
<a href="index.php?com=kontrak" style="border:0;color:#fff"><img src="templates/hrisrskm/icons/perpanjangan_kontrak.png" width="55px"><div style="width:100%; color:#fff;height:12px; padding-top:5px;background-color:grey;"><b>Perp. Kontrak</b></div></a></div>
<div style="width:90px;	height:72px; text-align:center;	border:#CDCDCD 1px solid;margin:2px;	float:left; font-size:9px; font-family:verdana;">
<a href="index.php?com=broadcast_message" style="border:0;color:#fff"><img src="templates/hrisrskm/icons/broadcast.png" width="55px"><div style="width:100%; color:#fff;height:12px; padding-top:5px;background-color:grey;"><b>Broadcast email</b></div></a></div>

<div style="width:90px;	height:72px; text-align:center;	border:#CDCDCD 1px solid;margin:2px;	float:left; font-size:9px; font-family:verdana;">
	<img src="templates/hrisrskm/icons/configuration.png" width="55px">
	<div style="width:100%; color:#fff;height:12px; padding-top:5px;background-color:grey;">
		<b>Konfigurasi</b>
	</div>
</div>

<div style="width:90px;	height:72px; text-align:center;	border:#CDCDCD 1px solid;margin:2px;	float:left; font-size:9px; font-family:verdana;">
<a href="login.php?logout='.md5(rand(0,1000)).'" style="border:0;color:#fff"><img src="templates/hrisrskm/icons/logout.png" width="55px"><div style="width:100%; color:#fff;height:12px; padding-top:5px;background-color:grey;"><b>Logout</b></div></a></div>
<div style="clear:both"></div>
</div>';



$design2 ='<div style="background:#fff;width:95%;height:100%;text-align:left;border:2px solid #CDCDCD;padding:3px;">
<div class="title_welcome_box">Data Karyawan</div><br/>   '.$aaaa.' 
 
</div>';


$design3 ='<div style="background:#fff;width:95%;height:100%;text-align:left;border:2px solid #CDCDCD;padding:3px;">
<div class="title_welcome_box">Perbandingan gender</div>    <br/> 
 <img src="index.php?com=graph&task=gender"   />
</div>';
$design4 ='<div style="background:#fff;width:98%;height:100%;text-align:left;border:2px solid #CDCDCD;padding:3px;">
 '.view_tab().'
</div>';
 
$view = '<div style="width:930px;text-align:center;">';
$view .= '<div style="width: 305px;padding:2px; height: 240px;text-align:left;float:left;">'.$design1.'</div>';
$view .= '<div style="width: 305px;padding:2px; height: 240px;text-align:left;float:left;">'.$design2.'</div> ';
$view .= '<div style="width: 305px;padding:2px; height: 240px;text-align:left;float:right;">'.$design3.'</div> ';
$view .= '<div style="clear:both"></div><br/>';
$view .= '<div style="width: 930px;padding:2px; height: 380px;text-align:left;">'.$design4.'</div> '; 
$view .= '<div style="clear:both"></div>';
$view .= '</div>';

return $view;
}

function thumb_photos_karyawan(){
my_set_code_css('
#thumb_list{
	width:850px; 
	margin:0px;
	text-align:center; 
} 
#thumb_list li.thumb_for_list{
	float:left;
	height:140px;
	margin:10px 10px 10px 10px;
	width:105px;
	border:1px solid;
	text-align:center; 
}
#thumb_list li.thumb_for_list img{
	float:center;  
	border:0; 
	padding:2px;
} 
');
	$path = 'files/services/birthdate_'.$_SESSION['user_id'].'.json';
	$file = file_get_contents($path, true);
	$datas = json_decode($file); 
	
	$view ='<ul id="thumb_list" >';
	foreach($datas as $data){
		$view .=' <li class="thumb_for_list"><div style="text-align:center;">
		<a href="index.php?com=karyawan&task=detail&id='.$data-> karyawan_id .'">
		<img src="'.$data->upload_path .'" height="85" border="0"/></a>
		<br/><span style="font:11px verdana"><b>'.display_name($data->name  ,12  ).'</b></span>
		<br/><span style="font:11px verdana">'.display_name( date('d/m/Y',strtotime($data->tanggal_lahir))  ,16  ).'</span>
		</div></li> ';
	}
	$view .='</ul>';
	 
	return $view;
}


function view_tab(){

	$aaaa = '<div style="width:820px;text-align:left;"> ';
	$aaaa .= calendarListView(); 
	$aaaa .= '</div>';
	$bbbb = thumb_photos_karyawan();
	$cccc = '<img src="index.php?com=graph&task=sebaran_usia" />';
	$dddd = '<img src="index.php?com=graph&task=trend" />'; 
	
	$tab_options = array('Galery Foto'=>'tab2','Kalender kerja'=>'tab1','Sebaran usia karyawan'=>'tab3','Trend aplikasi pelamar'=>'tab4' );
	$contents = array('tab2'=>$bbbb,'tab1'=>$aaaa,'tab3'=>$cccc ,'tab4'=>$dddd);
	return tab_page($tab_options , $contents );
}

function calendarListView(){
	my_component_load( 'list_kalender' ,false);
	$startdate =  date('Y-m-d',strtotime( date('Y-m-d'). " - 6 days")); 
	$enddate =  date('Y-m-d',strtotime( date('Y-m-d') ." + 6 days")); 
	$dates = list_kalender($startdate , $enddate  );
	$headers = array( 
		' '=>array('style'=>'border-bottom:2px solid;width:100px'),  
		'cuti'=>array('style'=>'border-bottom:2px solid;width:200px'),  
		'dinas luar'=>array('style'=>'border-bottom:2px solid;width:200px'), 
		'catatan'=>array('style'=>'border-bottom:2px solid;width:250px'), 
	);
	
	$row =array();
	$dnow =date('Y-m-d'); 
	foreach($dates as $date){
		$cuti=count_karyawan_cuti($date)	;
		$cuti_label = ($cuti > 0) ? $cuti. ' Orang' : '-';
		$spd=count_karyawan_spd($date)	;
		$spd_label = ($spd > 0) ? $spd. ' Orang' : '-';

		if( $date ==  $dnow):
			$row[] = array(   
				'tanggal'=>  '<b><font color="blue">'.$date.'</font></b>' , 
				'cuti'=> $cuti_label,  
				'dinas_luar'=> $spd_label, 
				'catatan'=>   '-', 
			); 
		else:
			$row[] = array(   
				'tanggal'=>  '<b>'.$date.'</b>' , 
				'cuti'=> $cuti_label,  
				'dinas_luar'=> $spd_label, 
				'catatan'=>   '-', 
			); 
		endif;
	}
	$datas = table_rows_plain($row); 
	return  table_builder_plain($headers , $datas ,  4 ,false );

}